package sf.view;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Set;

import javax.swing.JDialog;
import javax.swing.JPanel;

import sf.actions.SFActionManager;
import sf.controller.Controller;
import sf.controller.MatchTableModelFilter;
import sf.jsonloader.JsonLoader;
import sf.model.Context;
import sf.model.DBManager;

public class StandardForm extends JDialog {

	private static final long serialVersionUID = 1L;

	public final static int MODE_ADD = 1;
	public final static int MODE_EDIT = 2;
	public final static int MODE_SEARCH = 4;
	
	public final static int FROM_MENU = 8;
	public final static int FROM_ZOOM = 16;
	public final static int FROM_NEXT = 32;
	
	private int mode;

	private Context context;

	private Controller controller;
	private SFActionManager actionManager;

	private JPanel panelMain;
	private Toolbar toolbar;
	private Browser browser;
	private DetailsPane detailsPane;
	private StatusBar statusBar;

	public StandardForm(Context context, Frame parent) {
		super(parent, true);
	
		this.context = context;
		mode = context.getMode();
		
		// inicijalizuje json i ucitava iz fajla ...
		try {
			JsonLoader.getInstance().load("exported.json");
		} catch (FileNotFoundException e) {}

		controller = new Controller(this);
		actionManager = new SFActionManager(this);

		setTitle("eBroker :: " + (String)JsonLoader.getInstance().get(context.getTable().toUpperCase(), "name"));
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		// povezivanje sa bazom
		DBManager.init(context.getTable().toUpperCase());

		initMainPanel();
		initStatusBar();

		initToolbar();
		initBrowser();
		initDetailsPane();
		
		// postavlja formu u zeljeni mod; treba doraditi !
		setMode(context.getMode());

		if (context.getParams().containsKey("filter") && context.getParams().containsKey("primaryTargetFieldCode")) {			
			/* XXX: Ako vash filter bude filterisao JTable tako da ono po chemu treba da pretrazhujemo bude unique
			 * 		u svojoj koloni, onda mozhe da se skloni: keyset, filter, for...			 * 		
			 */
			Set<String> keyset = ((HashMap<String, Object>)context.getParam("filter")).keySet();			
			HashMap<String, Object> filter = (HashMap<String, Object>)context.getParam("filter");
			
			String columnNames[] = new String[keyset.size()+1];
			String columnValues[] = new String[keyset.size()+1];			
			int i = 0;				
			
			for (String key : keyset) {
				columnNames[i] = key;
				columnValues[i] = (String)filter.get(key); 
				i++;
			}
			
			columnNames[i] = (String)context.getParam("primaryTargetFieldCode");
			columnValues[i] = (String)context.getParam("primaryTargetFieldValue");
			
			MatchTableModelFilter mf = new MatchTableModelFilter(columnNames, columnValues, getBrowser().getTable().getModel());
			context.setSelectedRow( mf.getMatchedIndex() );			
		}

		//browser.setSelectedRow(context.getSelectedRow());

		pack();
	}

	private void initMainPanel() {
		panelMain = new JPanel();
		panelMain.setLayout(new BorderLayout());
		add(panelMain, BorderLayout.CENTER);
	}

	private void initToolbar() {
		toolbar = new Toolbar(this);
		panelMain.add(toolbar, BorderLayout.NORTH);
	}

	private void initBrowser() {
		browser = new Browser(this, context);
		panelMain.add(browser, BorderLayout.CENTER);
	}

	private void initDetailsPane() {
		detailsPane = new DetailsPane(this);
		panelMain.add(detailsPane, BorderLayout.SOUTH);
	}

	private void initStatusBar() {
		statusBar = new StatusBar();
		statusBar.setMode("Izmena");
		// statusBar.setInfo("<html>broj redova: <b>" + Integer.toString(browser.getTable().getRowCount()) + "</b></html>");
		add(statusBar, BorderLayout.SOUTH);
	}

	public Toolbar getToolbar() {
		return toolbar;
	}

	public Browser getBrowser() {
		return browser;
	}

	public DetailsPane getDetailsPane() {
		return detailsPane;
	}

	public StatusBar getStatusBar() {
		return statusBar;
	}

	public Controller getController() {
		return controller;
	}

	public SFActionManager getActionManager() {
		return actionManager;
	}

	public int getMode() {
		return mode;
	}

	public void setMode(int mode) {
		this.mode = mode;
		
		if ((mode&MODE_ADD) != 0)
			statusBar.setMode("Unos");
		else if ((mode&MODE_EDIT) != 0)
			statusBar.setMode("Izmena");
		else if ((mode&MODE_SEARCH) != 0)
			statusBar.setMode("Pretraga");
		
		toolbar.setupButtonModes();
		toolbar.setupSearchField();
	}

	public Context getContext() {
		return context;
	}
}
